// 
// @Author: Zhenwei Xiao
// @Description:身份存储接口
// @Version: 1.0.0
// @Date: 2021/3/20 上午9:09
// @Copyright: MIN-Group；国家重大科技基础设施——未来网络北大实验室；深圳市信息论与未来网络重点实验室
// 

const sqlite = require("./sqlite/db/db")
const com = require("../../common")

 

//默认是使用sqlite,修改存储引擎需要手动修改
var PersistPlugin = com.PersistPlugin.Sqlite

// 
// 存储一个Identity,生成identity结构体和存储分开
//
// @param identity *identity.Identity
// @param pass string
// @returns (bool, error)
// 

function PersistIdentity(identity, pass){
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.PersistIdentity(identity)
    }
    return [false, null]
}

// 
// 通过Identity的名称，删除一个Identity
//
// @param name string
// @param passwd string
// @returns (bool, error)
// 
function DeleteIdentityByNameFromStorage(name, passwd) {
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.DeleteIdentityByName(name)
    }
 
    return [false, null]
}

// 
// 通过Identity的名称，获得一个Identity对象
//
// @param name string
// @param passwd string
// @returns (*identity.Identity, error)
// 
function GetIdentityByNameFromStorage(name, passwd) {
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.GetIdentityByNameFromStorage(name)
    }

    return [null, null]
}

// 
// 获得所有的身份实例
//
// @param passwd string
// @returns ([]*identity.Identity, error)
// 
function GetAllIdentityFromStorage(passwd) {
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.GetAllIdentityFromStorage()
    }

    return [null, null]
}

// 
// 通过Identity的名称，设置默认的Identity
//
// @param name string, passwd string
// @returns (bool, error)
// 
function SetDefaultIdentityByNameInStorage(name, passwd) {
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.SetDefaultIdentityByNameInStorage(name)
    }

    return [false, null]
}

// 
// 获得当前系统默认使用的Identity对象
//
// @param passwd string
// @returns (*identity.Identity, error)
// 
function GetDefaultIdentityFromStorage(passwd)  {
    switch (PersistPlugin) {
    case com.PersistPlugin.Sqlite:
        return sqlite.GetDefaultIdentityFromStorage()
    }

    return [null, null]
}

module.exports = {
    PersistIdentity: PersistIdentity,
    GetIdentityByNameFromStorage: GetIdentityByNameFromStorage,
    GetAllIdentityFromStorage: GetAllIdentityFromStorage,
    DeleteIdentityByNameFromStorage: DeleteIdentityByNameFromStorage,
    SetDefaultIdentityByNameInStorage: SetDefaultIdentityByNameInStorage,
    GetDefaultIdentityFromStorage: GetDefaultIdentityFromStorage
}